package com.weixin.paleituo.activity;

import android.content.Context;
import android.content.Intent;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnItemClickListener;
import com.weixin.paleituo.R;
import com.weixin.paleituo.base.BaseActivity;
import com.weixin.paleituo.eventbus.SelectHouseEvent;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.ItemMyView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 绑定房子
 */
public class BindHouseActivity extends BaseActivity {


    @BindView(R.id.iv_back_activity_basepersoninfo)
    ImageView mIvBackActivityBasepersoninfo;
    @BindView(R.id.tv_title_activity_baseperson)
    TextView mTvTitleActivityBaseperson;
    @BindView(R.id.tv_small_title_layout_head)
    TextView mTvSmallTitleLayoutHead;
    @BindView(R.id.rl_head_root_activity_baseperson)
    RelativeLayout mRlHeadRootActivityBaseperson;
    @BindView(R.id.itenmyview_selectcity)
    ItemMyView mItenmyviewSelectcity;
    @BindView(R.id.itenmyview_shequ)
    ItemMyView mItenmyviewShequ;
    @BindView(R.id.itenmyview_buildingnumber)
    ItemMyView mItenmyviewBuildingnumber;
    @BindView(R.id.itenmyview_housenumber)
    ItemMyView mItenmyviewHousenumber;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_bind_house);

    }


    public static void myStartActvity(Context context) {
        Intent intent = new Intent(context, BindHouseActivity.class);
        context.startActivity(intent);

    }

    @Override
    protected void initData() {
        super.initData();
        EventBus.getDefault().register(this);
        mTvTitleActivityBaseperson.setText("选择房屋信息");
        mItenmyviewSelectcity.setRootBackgroub(UIUtils.getColor(R.color.c_fff5f5f5));
        mItenmyviewShequ.setRootBackgroub(UIUtils.getColor(R.color.c_fff5f5f5));
        mItenmyviewBuildingnumber.setRootBackgroub(UIUtils.getColor(R.color.c_fff5f5f5));
        mItenmyviewHousenumber.setRootBackgroub(UIUtils.getColor(R.color.c_fff5f5f5));

        mItenmyviewSelectcity.setViewLineColor(UIUtils.getColor(R.color.c_E6E6E6));
        mItenmyviewShequ.setViewLineColor(UIUtils.getColor(R.color.c_E6E6E6));
        mItenmyviewBuildingnumber.setViewLineColor(UIUtils.getColor(R.color.c_E6E6E6));
        mItenmyviewHousenumber.setViewLineColor(UIUtils.getColor(R.color.c_E6E6E6));


    }

    @OnClick(R.id.iv_back_activity_basepersoninfo)
    public void onMIvBackActivityBasepersoninfoClicked() {
        finish();
    }
    @OnClick(R.id.itenmyview_selectcity)
    public void onMItenmyviewSelectcityClicked() {

        SelectCityActivity.myStartActivity(this);
    }

    @OnClick(R.id.itenmyview_shequ)
    public void onMItenmyviewShequClicked() {
        if (isSelectCity){
        SelectCommunityActivity.myStartActivity(this);

        }else {
            AlertView alertView = new AlertView("提示", "请依次选择", "确定", null, null, this, AlertView.Style.Alert,
           null);
            alertView.show();
        }

    }

    @OnClick(R.id.itenmyview_buildingnumber)
    public void onMItenmyviewBuildingnumberClicked() {
        if (isSelectCity && isSelectCommunity){
        SelectBuildingActivity.myStartyActvity(this);

        }else {
            AlertView alertView = new AlertView("提示", "请依次选择", "确定", null, null, this, AlertView.Style.Alert,
                    null);
            alertView.show();
        }


    }

    @OnClick(R.id.itenmyview_housenumber)
    public void onMItenmyviewHousenumberClicked() {


        if (isSelectCity && isSelectCommunity&&isSelectBuilding){
        SelectHouseNumberActivity.myStartActiviyt(this);

        }else {
            AlertView alertView = new AlertView("提示", "请依次选择", "确定", null, null, this, AlertView.Style.Alert,
                    null);
            alertView.show();
        }

    }

    @OnClick(R.id.tv_check_sure_activity_bind_house)
    public void onMtvsure(){
    //CheckPhoneActivity.myStartActivity(this);
        if (isSelectCity&& isSelectCommunity && isSelectBuilding&& isSelectHouseNUmber){

            AlertView alertView = new AlertView("提示", "该房屋未预留手机号，请前往物业中心更新", null, null, new String[]{"确定"}, this, AlertView.Style.Alert,
                    new OnItemClickListener() {
                        @Override
                        public void onItemClick(Object o, int position) {
                            finish();
                            MainActivity.goGoodsShop(BindHouseActivity.this,3);
                        }
                    });
            alertView.show();




        }else {
            AlertView alertView = new AlertView("提示", "请先选择房屋信息", "确定", null, null, this, AlertView.Style.Alert,
                    null);
            alertView.show();


        }



    }

    private  boolean isSelectCity;
    private  boolean isSelectCommunity;
    private  boolean isSelectBuilding;
    private  boolean isSelectHouseNUmber;

@Subscribe(threadMode = ThreadMode.MAIN)
    public void selectHouseEvent(SelectHouseEvent selectHouseEvent){
        int type = selectHouseEvent.getType();
        if (type==1){
            mItenmyviewSelectcity.setDetails(selectHouseEvent.getContent());
            isSelectCity=true;
        }

        if (type==2){
            mItenmyviewShequ.setDetails(selectHouseEvent.getContent());
            isSelectCommunity=true;
        }
        if (type==3){
            mItenmyviewBuildingnumber.setDetails(selectHouseEvent.getContent());
            isSelectBuilding=true;
        }

        if (type==4){
            mItenmyviewHousenumber.setDetails(selectHouseEvent.getContent());
            isSelectHouseNUmber=true;
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
